export default {
	// hexToString
	hexToString(str) {
		if (str.length % 2 != 0) {
			return console.log('必须为偶数');
		}
		let buf = [];
		for (let i = 0; i < str.length; i += 2) {
			buf.push(parseInt(str.substring(i, i + 2), 16));
		}
		return this.readUTF(buf);
	},
	/**
	 * @param {Object} str
	 */
	bin2Hex(str) {
		let re = /[\u4E00-\u9FA5]/
		let ar = []
		for (let i = 0; i < str.length; i++) {
			let a = ''
			if (re.test(str.charAt(i))) { // 中文
				a = encodeURI(str.charAt(i)).replace(/%/g, '')
			} else {
				a = str.charCodeAt(i).toString(16)
			}
			ar.push(a)
		}
		str = ar.join('')
		return str
	},
	/**
	 * 页面返回
	 */
	pageBack() {
		let pages = getCurrentPages()
		if (pages.length === 1) {
			uni.reLaunch({
				url: '/pages/index/index'
			})
		} else {
			uni.navigateBack({
				delta: 1
			});
		}
	},
	/**
	 *	保存list值到本地缓存中
	 * @param {Object} key 
	 * @param {Object} list 数据
	 * @param {Object} expireInSeconds 过期时间单位s
	 */
	saveListWithExpire(key, list, expireInSeconds) {
		const now = Date.now();
		// 为每个元素添加过期时间
		const listWithExpire = list.map(item => ({
			...item,
			expire: now + expireInSeconds * 1000 // 设置过期时间
		}));

		uni.setStorage({
			key: key,
			data: JSON.stringify(listWithExpire), // 将整个列表转换为字符串进行存储
			success: function() {}
		});
	},
	/**
	 * 获取本地缓存数据
	 * @param {Object} key
	 */
	readListWithFilterExpire(key) {
		return new Promise((resolve, reject) => {
			uni.getStorage({
				key: key,
				success: function(res) {
					if (!res || !res.data) {
						resolve([])
					}
					const listWithExpire = JSON.parse(res.data); // 将字符串转换回对象列表
					// 当前时间
					const now = Date.now();
					// 过滤出未过期的数据
					const validData = listWithExpire.filter(item => now <= item.expire);
					if (validData.length < listWithExpire.length) {
						// 如果有过期的数据，可以选择更新存储，移除过期项
						uni.setStorage({
							key: key,
							data: JSON.stringify(validData),
							success: function() {
								console.log('已移除过期数据');
							}
						});
					}
					resolve(validData); // 返回有效的数据
				},
				fail: function(err) {
					resolve([])
				}
			});
		});
	},
	parseTime(v,format){
		format = format ? format : "yyyy-MM-dd HH:mm:ss"
		if (!v) return "";
		var d = v;
		if (typeof v === 'string') {
			if (v.indexOf("/Date(") > -1)
				d = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
			else
				d = new Date(Date.parse(v.replace(/-/g, "/").replace("T", " ").split(".")[0]));
		} else {
			d = new Date(v)
		}
		var o = {
			"M+": d.getMonth() + 1,
			"d+": d.getDate(),
			"h+": d.getHours(),
			"H+": d.getHours(),
			"m+": d.getMinutes(),
			"s+": d.getSeconds(),
			"q+": Math.floor((d.getMonth() + 3) / 3),
			"S": d.getMilliseconds()
		};
		if (/(y+)/.test(format)) {
			format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
		}
		for (var k in o) {
			if (new RegExp("(" + k + ")").test(format)) {
				format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" +
						o[k])
					.length));
			}
		}
		return format; 
	}
}